﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;

/// <summary>
/// Summary description for ProviderCompanyLinkDB
/// </summary>
/// 
namespace CE.Provider
{
    public class ProviderCompanyLinkDB
    {
        public ProviderCompanyLinkDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(ProviderCompanyLink procomlink)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocProviderCompanyLinkInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@ProviderCompanyId", procomlink.ProviderCompanyId);
            _sPara.Add("@CEID", procomlink.CEID);
            _sPara.Add("@UpdateFlag", procomlink.UpdateFlage);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public DataTable GetProviderList(Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("ProviderList");
            string _spName = "sprocProviderCompanyLinkSelectSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@CEID", "0");
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        public ProviderCompanyLink GetProviderCompanyId(Int64 ProviderId)
        {
            ProviderCompanyLink _comlink = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCompanyLinkSelectSingleItem";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCompanyId", "0");
            _sPara.Add("@CEID", ProviderId);
            #endregion

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _comlink = new ProviderCompanyLink();
                    _comlink = FillDataRecord(_dt, 0);
                }
            }
            return _comlink;
        }

        private ProviderCompanyLink FillDataRecord(DataTable myDataTable, int RowId)
        {
            ProviderCompanyLink myComLink = new ProviderCompanyLink();
            myComLink.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyID"].ToString());
            myComLink.CEID = Convert.ToInt64(myDataTable.Rows[RowId]["CEID"].ToString());
            return myComLink;
        }
    }
}